package GUI;

import CDB.Empleados;
import CDB.Permisos;
import Negocios.ControlEmployee;
import Utilidades.Validaciones;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author ISC Erick
 */
public class AltaEmpleado extends javax.swing.JInternalFrame {

    Date d = new Date();
    private String mensajeVal = null;
    private boolean updatePermisos = false;
    private boolean updateEmpleado = false;
    private int idPermisosUpdate;
    private int idEmpleadoUpdate;

    public AltaEmpleado() {
        initComponents();
        cargarPermisos();
    }

    public AltaEmpleado(Permisos per) {
        initComponents();
        visualizarEdicionPermisos(per);
        updatePermisos = true;
        idPermisosUpdate = per.getIdpermisos();
    }

    public AltaEmpleado(Empleados emp) {
        initComponents();
        idEmpleadoUpdate = emp.getIdempleado();
        updateEmpleado = true;
        visualizarEdicionEmpleado(emp);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jdtFecha = new com.toedter.calendar.JDateChooser();
        jLabel9 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JTextField();
        jspPermisos = new javax.swing.JScrollPane();
        panPermisos = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(true);
        setTitle("Sales Tea Dispatcher - Alta de un empleado.");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/Employee.png"))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel1.setText("En esta ventana se agregan los empleados recién contratados.");

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/Cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/save.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/Employee.png"))); // NOI18N

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jLabel2.setText("Nombre del empleado:");

        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombreKeyTyped(evt);
            }
        });

        jLabel3.setText("Direccion:");

        txtDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDireccionKeyTyped(evt);
            }
        });

        jLabel4.setText("Teléfono:");

        txtTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTelefonoKeyTyped(evt);
            }
        });

        jLabel5.setText("Fecha de contratación:");

        jdtFecha.setDate(d);
        jdtFecha.setDateFormatString("yyyy/MM/dd");

        jLabel9.setText("Password:");

        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPasswordKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDireccion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTelefono, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jdtFecha, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                            .addComponent(txtPassword))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jdtFecha, txtDireccion, txtNombre, txtTelefono});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jdtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jspPermisos.setBackground(new java.awt.Color(255, 255, 255));
        jspPermisos.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        panPermisos.setBackground(new java.awt.Color(255, 255, 255));
        panPermisos.setMaximumSize(new java.awt.Dimension(355, 143));
        panPermisos.setMinimumSize(new java.awt.Dimension(355, 143));
        panPermisos.setName(""); // NOI18N

        javax.swing.GroupLayout panPermisosLayout = new javax.swing.GroupLayout(panPermisos);
        panPermisos.setLayout(panPermisosLayout);
        panPermisosLayout.setHorizontalGroup(
            panPermisosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 476, Short.MAX_VALUE)
        );
        panPermisosLayout.setVerticalGroup(
            panPermisosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 210, Short.MAX_VALUE)
        );

        jspPermisos.setViewportView(panPermisos);

        jLabel8.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel8.setText("Seleccione los permisos que tendra este usuario:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel8)
                            .addComponent(jspPermisos, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)
                        .addContainerGap())))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancelar, btnGuardar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel6))
                    .addComponent(jLabel7)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspPermisos, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void visualizarEdicionEmpleado(Empleados emp) {
        //Colocar los datos del empleado al que se editara
        txtNombre.setText(emp.getNombre());
        txtDireccion.setText(emp.getDireccion());
        txtPassword.setText(emp.getPassword());
        txtTelefono.setText(emp.getTelefono());
        jdtFecha.setDate(emp.getFechacontratacion());
        panPermisos.setLayout(new FlowLayout(FlowLayout.CENTER));
        panPermisos.add(new JLabel("Para modificar los permisos, hágalo desde el catálogo de permisos."));
    }
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        try {
            this.setClosed(true);
            System.gc();
        } catch (PropertyVetoException ex) {
            this.dispose();
            System.gc();
        }
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void visualizarEdicionPermisos(Permisos per) {
        cargarPermisos();
        //Colocar los datos del empleado al que se editara
        txtNombre.setText(per.getEmpleados().getNombre());
        txtDireccion.setText(per.getEmpleados().getDireccion());
        txtPassword.setText(per.getEmpleados().getPassword());
        txtTelefono.setText(per.getEmpleados().getTelefono());
        jdtFecha.setDate(per.getEmpleados().getFechacontratacion());
        //Bloquear datos para evitar que se cambie de empleado
        txtDireccion.setEnabled(false);
        txtNombre.setEnabled(false);
        txtPassword.setEnabled(false);
        txtTelefono.setEnabled(false);
        jdtFecha.setEnabled(false);
        //Activar los permisos que se tienen activados anteriormente
        mostrarPermisos(per);
        
    }

    private void mostrarPermisos(Permisos permisos) {
        if (permisos.isPuntoventa()) {
            JPanel panel = (JPanel) panPermisos.getComponent(0);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isRepaldodb()) {
            JPanel panel = (JPanel) panPermisos.getComponent(1);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isRestaurardb()) {
            JPanel panel = (JPanel) panPermisos.getComponent(2);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isAltaproducto()) {
            JPanel panel = (JPanel) panPermisos.getComponent(3);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isElimeditproducto()) {
            JPanel panel = (JPanel) panPermisos.getComponent(4);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isAltaproveedor()) {
            JPanel panel = (JPanel) panPermisos.getComponent(5);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isElimeditproveedor()) {
            JPanel panel = (JPanel) panPermisos.getComponent(6);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isAltaempleado()) {
            JPanel panel = (JPanel) panPermisos.getComponent(7);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isElimeditempleado()) {
            JPanel panel = (JPanel) panPermisos.getComponent(8);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isReporteventas()) {
            JPanel panel = (JPanel) panPermisos.getComponent(9);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isReporteinventario()) {
            JPanel panel = (JPanel) panPermisos.getComponent(10);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCatalogoproductos()) {
            JPanel panel = (JPanel) panPermisos.getComponent(11);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCatalogoempleados()) {
            JPanel panel = (JPanel) panPermisos.getComponent(12);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCatalogoproveedor()) {
            JPanel panel = (JPanel) panPermisos.getComponent(13);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCatalogostock()) {
            JPanel panel = (JPanel) panPermisos.getComponent(14);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCatalogocategorias()) {
            JPanel panel = (JPanel) panPermisos.getComponent(15);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCatalogosubcategorias()) {
            JPanel panel = (JPanel) panPermisos.getComponent(16);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCatalogopermisos()) {
            JPanel panel = (JPanel) panPermisos.getComponent(17);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isReporteproductos()) {
            JPanel panel = (JPanel) panPermisos.getComponent(18);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isReporteempleados()) {
            JPanel panel = (JPanel) panPermisos.getComponent(19);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isAltacategoria()) {
            JPanel panel = (JPanel) panPermisos.getComponent(20);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isElimcategoria()) {
            JPanel panel = (JPanel) panPermisos.getComponent(21);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isAltasubcategoria()) {
            JPanel panel = (JPanel) panPermisos.getComponent(22);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isElimsubcategoria()) {
            JPanel panel = (JPanel) panPermisos.getComponent(23);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }
        if (permisos.isCortedecaja()) {
            JPanel panel = (JPanel) panPermisos.getComponent(24);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(true);
        }

    }

    private void cargarPermisos() {
        String[] permisos = {"Punto de venta", "Respaldar BD", "Restaurar DB", "Alta producto", "Eliminar/Editar producto",
            "Alta proveedor", "Eliminar/Editar proveedor", "Alta empleado", "Eliminar/Editar empleado", "Reporte ventas",
            "Reporte inventario", "Catalogo productos", "Catalogo empleados", "Catalogo proveedores", "Catalogo Stock",
            "Catalogo Categorias", "Catalogo Subcategorias", "Catalogo permisos", "Reporte productos", "Reporte empleados",
            "Alta categoria", "Eliminar categoria", "Alta Subcategoria", "Eliminar Subcategoria", "Corte de caja",
            "Devoluciones"};
        panPermisos.setVisible(false);
        panPermisos.setLayout(new GridLayout(0, 3, 3, 3));
        for (int i = 0; i < permisos.length; i++) {
            panPermisos.add(formarPermisos(permisos[i]));
        }
        panPermisos.setVisible(true);
    }

    private JPanel formarPermisos(String permiso) {
        JPanel subcat;
        subcat = new JPanel(new BorderLayout());

        JLabel nombre;
        nombre = new JLabel(permiso);
        JCheckBox activa = new JCheckBox("Permitir", false);

        activa.setForeground(Color.WHITE);
        activa.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        activa.setName(permiso);
        nombre.setForeground(Color.WHITE);
        nombre.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        subcat.add(nombre, BorderLayout.NORTH);
        subcat.add(activa, BorderLayout.SOUTH);
        subcat.setBackground(new java.awt.Color(0, 150, 0));

        return subcat;
    }

    public void cleanFields() {

        //Limpiando los campos
        txtDireccion.setText("");
        txtNombre.setText("");
        txtTelefono.setText("");
        jdtFecha.setDate(d);
        txtPassword.setText("");
        txtNombre.requestFocus();

        //regresar las checkbox a su estado defoult
        int numComponentes = panPermisos.getComponentCount();

        for (int i = 0; i < numComponentes; i++) {
            JPanel panel = (JPanel) panPermisos.getComponent(i);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            chk.setSelected(false);
        }

    }

    public boolean validaciones() {
        Validaciones val = new Validaciones();
        boolean valida = true;
        if (val.isEmpty(txtNombre)) {
            valida = false;
            mensajeVal = "El campo -Nombre producto- esta vacio";
        } else if (val.isEmpty(txtDireccion)) {
            valida = false;
            mensajeVal = "El campo -Direccion- esta vacio";
        } else if (val.isEmpty(txtTelefono)) {
            valida = false;
            mensajeVal = "El campo -Telefono- esta vacio";
        } else if (val.isEmpty(txtPassword)) {
            valida = false;
            mensajeVal = "El campo -Password- esta vacio";
        }
        return valida;
    }

    private List obtenerPermisos() {

        List<Boolean> permisos = new ArrayList<>();
        int numPermisos = panPermisos.getComponentCount();

        for (int i = 0; i < numPermisos; i++) {
            JPanel panel = (JPanel) panPermisos.getComponent(i);
            JCheckBox chk = (JCheckBox) panel.getComponent(1);
            if (chk.isSelected()) {
                permisos.add(true);
            } else {
                permisos.add(false);
            }
        }
        return permisos;
    }

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed

        ControlEmployee control = new ControlEmployee();
        Date date = jdtFecha.getDate();
        if (validaciones()) {
            try {
                if (updatePermisos == false && updateEmpleado == false) {
                    //enviar a control para que guarde en la DB el nuevo empleado
                    control.add(txtNombre.getText(), txtDireccion.getText(),
                            txtTelefono.getText(), date, obtenerPermisos(), txtPassword.getText());
                    JOptionPane.showMessageDialog(this, "El empleado "
                            + txtNombre.getText() + " ha sido guardado correctamente.",
                            "Guardado con exito", 1);
                    cleanFields();
                } else if (updatePermisos == true) {
                    //enviar a control para que guarde en la DB el nuevo empleado
                    control.update(txtNombre.getText(), txtDireccion.getText(),
                            txtTelefono.getText(), date, obtenerPermisos(), txtPassword.getText(), idPermisosUpdate);
                    JOptionPane.showMessageDialog(this, "El empleado "
                            + txtNombre.getText() + " ha sido actualizado correctamente.",
                            "Actualizado con exito", 1);
                    cleanFields();
                    this.dispose();
                } else if (updateEmpleado == true) {
                    //enviar a control para que guarde en la DB el nuevo empleado
                    control.update(txtNombre.getText(), txtDireccion.getText(),
                            txtTelefono.getText(), date, txtPassword.getText(), idEmpleadoUpdate);
                    JOptionPane.showMessageDialog(this, "El empleado "
                            + txtNombre.getText() + " ha sido actualizado correctamente.",
                            "Actualizado con exito", 1);
                    this.dispose();
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "A ocurrio un error al guardar"
                        + " a el empleado, el error es:" + "\n" + e,
                        "A ocurrido un error.", 0);
            }
        } else {
            JOptionPane.showMessageDialog(this, mensajeVal, "Informacion errronea", 2);
        }


    }//GEN-LAST:event_btnGuardarActionPerformed

    private void txtTelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTelefonoKeyTyped
        char key = evt.getKeyChar();
        if (((key < '0' || key > '9') && (key != '\b') && (key != '\n')) || (txtTelefono.getText().length() > 19)) {
            evt.consume();
            JOptionPane.showMessageDialog(this, "Solo se permiten numeros en "
                    + "este campo", "Lestras no permitidas", 0);
        }
    }//GEN-LAST:event_txtTelefonoKeyTyped

    private void txtNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyTyped
        if (txtNombre.getText().length() > 29) {
            evt.consume();
            JOptionPane.showMessageDialog(this, "Solo se permite un largo maximo"
                    + " de 20 letras en este campo.", "Maximo de letras alcanzado", 0);
        }
    }//GEN-LAST:event_txtNombreKeyTyped

    private void txtDireccionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDireccionKeyTyped
        if (txtDireccion.getText().length() > 24) {
            evt.consume();
            JOptionPane.showMessageDialog(this, "Solo se permite un largo maximo"
                    + " de 24 letras en este campo.", "Maximo de letras alcanzado", 0);
        }
    }//GEN-LAST:event_txtDireccionKeyTyped

    private void txtPasswordKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyTyped
        if (txtPassword.getText().length() > 15) {
            evt.consume();
            JOptionPane.showMessageDialog(this, "Solo se permiten 16 caracteres "
                    + "en este campo.", "Largo maximo alcansado", 0);
        }
    }//GEN-LAST:event_txtPasswordKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    public com.toedter.calendar.JDateChooser jdtFecha;
    private javax.swing.JScrollPane jspPermisos;
    private javax.swing.JPanel panPermisos;
    public javax.swing.JTextField txtDireccion;
    public javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtPassword;
    public javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
